package com.ithema.edu.info.manager.controller;

import com.ithema.edu.info.manager.domain.Teacher;
import com.ithema.edu.info.manager.service.TeacherService;

import java.util.ArrayList;
import java.util.Scanner;



public class TeacherController {
    private Scanner sc = new Scanner(System.in);
    TeacherService se=new TeacherService();
    public void start(){

        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            int choose = sc.nextInt();
            switch (choose){
                case 1:
                    addTeachers();
                    //System.out.println("添加老师");
                    break;
                case 2:
                    deleteTeachers();
                    //System.out.println("删除老师");
                    break;
                case 3:
                    updateTeacher();
                    //System.out.println("修改老师");
                    break;
                case 4:
                    queryTeacher();
                    //System.out.println("查看老师");
                    break;
                case 5:
                    System.out.println("退出老师管理系统");
                    return;
                default:
                    System.out.println("您输入的有误！请重新输入！");
                    break;
            }
        }
    }




    //.......................................
    /* 添加老师*/
    //.......................................
    public  void addTeachers() {
        System.out.println("请输入您要添加的老师工号：");
        String id = sc.next();
        System.out.println("请输入您要添加的老师姓名：");
        String name = sc.next();
        System.out.println("请输入您要添加的老师年龄：");
        int age = sc.nextInt();
        System.out.println("请输入您要添加的老师手机号：");
        String phone = sc.next();
        Teacher teacher=new Teacher(id, name, age, phone);

        se.addTeachers(teacher);
        System.out.println("添加成功！");
    }

    //.......................................
    /* 删除老师*/
    //.......................................
    public  void deleteTeachers() {
        while (true) {
            System.out.println("请输入您要删除的老师工号：");
            String id=sc.next();
            if ("over".equals(id)) {
                return;
            }

            boolean b = se.deleteTeachers(id);
            if(b){
                System.out.println("删除成功！");
                break;
            }else{
                System.out.println("您输入的工号有误！请重新输入！若退出请输入over!");
            }
        }

    }

    //.......................................
    /* 修改老师*/
    //.......................................
    public  void updateTeacher() {
        String id;
        int exists;
        while (true) {
            System.out.println("请输入您要修改的老师工号：");
            id = sc.next();
            if ("over".equals(id)) {
                return;
            }
            exists = se.isExists(id);
            if (exists == -1) {
                System.out.println("您输入的工号不存在，请重新输入！若退出请输入over!");
            } else {
                break;
            }
        }
            System.out.println("请输入新的学生姓名：");
            String name = sc.next();
            System.out.println("请输入新的学生年龄：");
            int age = sc.nextInt();
            System.out.println("请输入新的学生手机号：");
            String phone = sc.next();
            Teacher newteacher=new Teacher(id, name, age, phone);
            se.updateTeacher(exists,newteacher);
            System.out.println("修改成功！");

    }

    //.......................................
    /* 查看老师*/
    //.......................................
    public  void queryTeacher(){
        ArrayList<Teacher> List=se.queryTeacher();
        if(List.size()==0){
            System.out.println("无数据！请添加后查看！");
        } else{
            System.out.println("工号" + "\t\t" + "姓名" + "\t" + "年龄" + "\t\t" + "手机号");
            for (int i = 0; i < List.size(); i++) {
                Teacher teacher=List.get(i);
                System.out.println(teacher.getId() + "\t" + teacher.getName() + "\t" + teacher.getAge() + "\t" + teacher.getPhone());
            }
        }

    }
}
